<script setup lang="ts">
import { getCurrentInstance, onMounted, ref, shallowRef } from 'vue'
import DetailTable from '@/views/project-update/project-ledger/general-ledger/detail-table.vue'
import { getProjectLedgerTotalDetail } from '@/api/project-update/project-ledger'
import Download from '@/views/project-update/components/download.vue'
import { PERMISSIONS } from '@/common/user'

const vm = getCurrentInstance()

const route = vm.proxy.$route
const router = vm.proxy.$router
const currentUser = vm.proxy.$store.state.user.currentUser

const handleCancel = () => {
  router.go(-1)
}

const params = route.query
const { regionId, year, month, status } = params

let projectId = ''

const detailTableRef = ref(null)

const recordArr = shallowRef([])

onMounted(() => {
  if (regionId) {
    getProjectLedgerTotalDetail({ regionId, year, month }).then((res) => {
      if (res.success) {
        // console.log(res.data)
        const tableData = []
        const record = []
        const len = res.data.length
        for (let i = 0; i < len; i++) {
          const {
            projectMonthPlanInfo,
            projectMonthProjectCompleteInfoList,
            projectMonthBankInfoList,
            projectMonthFinanceInfoList,
            projectMonthProblemInfoList,
          } = res.data[i]
          const tempList = []
          record.push({
            pkid: i + '',
            projectCompleteInfoListLength:
              projectMonthProjectCompleteInfoList?.length || 0,
            projectBankInfoListLength: projectMonthBankInfoList?.length || 0,
            projectFinanceInfoListLength:
              projectMonthFinanceInfoList?.length || 0,
            projectProblemInfoListLength:
              projectMonthProblemInfoList?.length || 0,
          })

          if (projectMonthPlanInfo) {
            projectMonthPlanInfo.projectName2 = projectMonthPlanInfo.projectName
            projectMonthPlanInfo.projectName = ''
            tempList.push(projectMonthPlanInfo)
          }
          if (projectMonthProjectCompleteInfoList?.length) {
            projectMonthProjectCompleteInfoList.forEach((item, index) => {
              if (tempList[index]) {
                tempList[index] = {
                  ...tempList[index],
                  ...item,
                  addInvestment: '',
                  addInvestment2: item.addInvestment,
                  regionId,
                  regionName: tempList[index].regionName,
                }
              } else {
                tempList.push(item)
              }
            })
          }
          if (projectMonthBankInfoList?.length) {
            projectMonthBankInfoList.forEach((item, index) => {
              if (tempList[index]) {
                tempList[index] = {
                  ...tempList[index],
                  ...item,
                }
              } else {
                tempList.push(item)
              }
            })
          }
          if (projectMonthFinanceInfoList?.length) {
            projectMonthFinanceInfoList.forEach((item, index) => {
              if (tempList[index]) {
                tempList[index] = {
                  ...tempList[index],
                  ...item,
                }
              } else {
                tempList.push(item)
              }
            })
          }
          if (projectMonthProblemInfoList?.length) {
            projectMonthProblemInfoList.forEach((item, index) => {
              if (tempList[index]) {
                tempList[index] = {
                  ...tempList[index],
                  ...item,
                }
              } else {
                tempList.push(item)
              }
            })
          }
          if (tempList[0]) {
            tempList[0].pkid = i + ''
          }
          tableData.push(...tempList)
        }

        recordArr.value = record
          .map((item) => {
            const max = Math.max(
              item.projectCompleteInfoListLength,
              item.projectBankInfoListLength,
              item.projectFinanceInfoListLength,
              item.projectProblemInfoListLength
            )
            return {
              ...item,
              rowSpan: max || 1,
            }
          })
          .filter((item) => item.rowSpan > 1)
        // console.log(recordArr.value)
        // console.log(tableData)
        detailTableRef.value.inputData(tableData)
      }
    })
  }
})

const detailDownloadParams = [
  {
    regionId,
    year,
    month,
    status,
  },
]
</script>

<template>
  <div class="add-month-report">
    <div class="nav">
      <div class="breadcrumb">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/project-update/general-ledger' }"
            ><span class="breadcrumb-title">列表</span></el-breadcrumb-item
          >
          <el-breadcrumb-item>详情</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>
    <div class="content">
      <div class="content-main">
        <!--        <detail-table ref="detailTableRef" :status="'detail'" />-->
        <detail-table
          ref="detailTableRef"
          :year="year"
          :recordArr="recordArr"
        />
      </div>
      <div class="buttons">
        <el-button class="cancel" @click="handleCancel">取消</el-button>
        <download
          type="month-plan"
          :params="detailDownloadParams"
          :permission="PERMISSIONS.总台账详情下载"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.add-month-report {
  width: calc(100% - 40px);
  height: calc(100% - 40px);
  padding: 20px;
  background-color: rgba(244, 248, 254, 1);

  .content {
    width: calc(100% - 40px);
    height: calc(100% - 70px);
    position: absolute;
    top: 65px;
    bottom: 0;

    .divider {
      width: 100%;
      height: 1px;
      background: #e7f0ff;
      border-radius: 1px 1px 0px 0px;
      margin: 20px 0;
    }

    .content-main {
      height: calc(100% - 100px);
      background: #ffffff;
      box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.1);
      border-radius: 2px;
      padding: 13px 16px 13px 16px;
      overflow-y: auto;
    }

    .buttons {
      padding: 10px;
      display: flex;
      justify-content: center;
      background: #ffffff;
      box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.1);

      .el-button {
        width: 110px;
      }

      .cancel {
        background: #ffffff;
        border-radius: 4px;
        border: 1px solid #e7f0ff;
        font-weight: 400;
        font-size: 16px;
        color: #8894a8;
      }

      .save {
        //background: #10a6dd;
        border-radius: 4px;
        //color: #ffffff;
      }

      .publish {
        //background: #ffffff;
        border-radius: 4px;
        //color: #10a6dd;
        //border: 1px solid #10a6dd;
      }
    }
  }
}
</style>
